<template>
  <div class="main">
    <div>
      <!-- 二级路由 -->
      <router-view></router-view>
    </div>
    <!-- 菜单，首页，购物车，我的  导航栏 -->
    <van-tabbar
      active-color="#0C34BA"
      v-model="activeIndex"
      inactive-color="#646566"
      route
    >
      <van-tabbar-item
        v-for="(item, index) in ficon"
        :key="index"
        :to="item.to"
        :badge="item.badge"
      >
        <span>{{ item.title }}</span>
        <template #icon="props">
          <img :src="props.active ? item.activeIcon : item.inactiveIcon" />
        </template>
      </van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script>
export default {
  name: "Main",
  created() {
    this.getnum();
  },
  methods: {
    // 查询用户所有购物车数据
    getnum() {
      this.axios({
        method:"get",
        url:'/findAllShopcart',
      }).then(res=>{
        
        let a = [];
          if (res.data.code == 5000) {
            res.data.result.forEach((v) => {
              // 如果查询到商品有数量则放进a数组中
              if (v.count != 0) {
                a.push(v);
              }
            }); 
           if(a.length==0) return;
          // 记录购物车的商品条数
           this.ficon[2].badge=a.length;
          }
      }).catch(err=>{
        
      })
    }
  },
  data() {
    return {
      activeIndex: 0,
      ficon: [
        {
          title: "首页",
          activeIcon: require("../assets/images/home_active.png"),
          inactiveIcon: require("../assets/images/home.png"),
          to: { name: "HomePage" },
        },
        {
          title: "菜单",
          activeIcon: require("../assets/images/menu_active.png"),
          inactiveIcon: require("../assets/images/menu.png"),
          to: { name: "Menu" },
        },
        {
          title: "购物袋",
          activeIcon: require("../assets/images/shopbag_active.png"),
          inactiveIcon: require("../assets/images/shopbag.png"),
          to: { name: "ShopBag" },
          badge:''
        },
        {
          title: "我的",
          activeIcon: require("../assets/images/my_active.png"),
          inactiveIcon: require("../assets/images/my.png"),
          to: { name: "My" },
        },
      ],
    };
  },
};
</script>

<style lang="less" scoped>
.box {
  width: 200px;
  height: 300px;
  background: pink;
}
</style>
